﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Web;

namespace SmartSpace.News.UI.Pages
{
    public partial class EditArticle : System.Web.UI.Page
    {
        private Guid _articleId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            _articleId = Guid.Parse(Request.QueryString["news_article_id"]);

            if (!IsPostBack)
            {
                SetValueForUI();
            }
        }

        private void SetValueForUI()
        {
            NewsService service = new NewsService();
            Article article = service.GetArticle(_articleId).Data;
            if (article != null)
            {
                _ctlTitle.Text = article.Title;
                _ctlDesc.Text = article.Description;
                _ctlContent.Text = article.Content;
                _ctlCode.Text = article.Code;
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            NewsService service = new NewsService();
            Article article = service.GetArticle(_articleId).Data;
            if (article != null)
            {
                article.Title = _ctlTitle.Text;
                article.Description = _ctlDesc.Text;
                article.Content = _ctlContent.Text;
                service.SaveArticle(article);
            }

            SsPage.Return(this, new { id = article.Id });
        }
    }
}